package com.rongke.web.api;

import com.rongke.commons.JsonResp;
import com.rongke.commons.PageDto;
import com.rongke.model.UserPhone;
import com.rongke.service.UserPhoneService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @UserPhoneController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/userPhone")
@Transactional
@CrossOrigin
public class UserPhoneController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserPhoneService userPhoneService;

    /**
     * @添加
     * @param userPhone
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addUserPhone(@RequestBody UserPhone userPhone){
        log.debug("添加");
        userPhoneService.insert(userPhone);
        return JsonResp.ok(userPhone);
    }

    /**
     * @修改
     * @param userPhone
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateUserPhone(@RequestBody UserPhone userPhone){
        log.debug("修改");
        userPhoneService.updateById(userPhone);
        return JsonResp.ok(userPhone);
    }
    /**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectUserPhone(Long id){
        log.debug("查找");
        UserPhone userPhone = userPhoneService.selectById(id);
        return JsonResp.ok(userPhone);
    }

    /**
     * @分页查询手机认证
     * @param
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/findByPage", method = RequestMethod.GET)
    public JsonResp findByPage(Integer pageNo, Integer pageSize,String phone,Integer status,String realName){
        log.debug("分页查询手机认证");
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("phone",phone);
        map.put("realName",realName);
        map.put("status",status);
        Integer count = userPhoneService.selectCount(map);
        List<UserPhone> list = userPhoneService.selectByPage(map);

        return JsonResp.ok(new PageDto(pageNo,pageSize,list,count));
    }




}
